﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Reflection;
using DAL;
using UtilLibrary;
using Util;

namespace Biz
{
    public class TaskService : BaseService
    {
        private BizTask task;
        public TaskService(BizTask pTask)
        {
            this.task = pTask;
            this.bizBaseEntity = pTask;
        }

        /// <summary>
        /// 启动一个任务
        /// </summary>
        public bool Start() {
            if (this.task.TaskStatus == TaskStatus.Processing)
                return false;

            this.task.TaskStatus = TaskStatus.Processing;
            
            this.task.Evaluate = DateTime.Now < task.PlanFinishDate? ScheduleStatus.Normal : ScheduleStatus.Delay;
            this.task.GoForward();
            //TODO:Add the save to DB
            return true;
        }
        /// <summary>
        /// 任务进入下一个阶段（下一个责任人时代）
        /// </summary>
        public bool Next()
        {
            //if (this.task.Status != TaskStatus.Processing)
            //    return false;
            // 已经到了最后一个步骤
            if (this.task.TaskWF.isLastOne())
            {
                this.task.TaskStatus = TaskStatus.Finish;
            }
            // 还是在中间步骤
            else
            {
                this.task.Evaluate = DateTime.Now < task.PlanFinishDate ? ScheduleStatus.Normal : ScheduleStatus.Delay;
                this.task.GoForward();
            }

            this.task.Update();

            return true;
        }

        public void Finish()
        {
            this.task.Finish();
        }

        public static List<BizTask> GetAllEntities()
        {
            List<BizTask> ret = new List<BizTask>();
            Model1Container container = new Model1Container();
            var query = from t in container.task
                        select t;

            foreach (task item in query)
            { 
                BizTask bt = new BizTask();
                UtilLibrary.DbUtil.DataEntityToBizObjTrans(item, bt);
                bt.TaskWF = BizWorkflow.GetWorkflowByTaskID(item.Id);
                ret.Add(bt);
            }

            return ret;
        }

        /// <summary>
        /// 根据条件检索任务
        /// </summary>
        /// <param name="pTaskName">任务名称</param>
        /// <param name="pProjId">项目Id</param>
        /// <param name="pStatus">任务状态</param>
        /// <param name="pEmpId">员工Id</param>
        /// <returns></returns>
        public static List<BizTask> GetEntities(String pTaskName, int? pProjId, String pStatus)
        {
            List<BizTask> ret = new List<BizTask>();
            Model1Container container = new Model1Container();
            var query = from t in container.task
                        where (string.IsNullOrEmpty(pTaskName) || t.TaskName.Contains(pTaskName))
                        &&( pProjId == 0 || t.ProjId == pProjId)
                        &&(string.IsNullOrEmpty(pStatus) || t.TaskStatus == pStatus)
                        select t;

            foreach (task item in query)
            {
                BizTask bt = new BizTask();
                UtilLibrary.DbUtil.DataEntityToBizObjTrans(item, bt);
                bt.TaskWF = BizWorkflow.GetWorkflowByTaskID(item.Id);
                ret.Add(bt);
            }

            return ret;
        }

        public void CreateWFAndNodes(List<string> pStepNames, List<int> pEmpIDs)
        {
            task.Create();

            BizWorkflow wf = new BizWorkflow(task.Id);
            wf.Create();
            for (int i = 0; i < pStepNames.Count; i++)
            {
                BizWorkflowSequeuece activity = new BizWorkflowSequeuece(i + 1, pEmpIDs[i], pStepNames[i]);
                wf.AddNode(activity);
            }
            task.TaskWF = wf;
            this.Start();
        }

        public void UpdateWFAndNodes(List<string> pStepNames, List<int> pEmpIDs)
        {
            task.Update();

            BizWorkflow wf = BizWorkflow.GetWorkflowByTaskID(task.Id);
            // 删除所有的节点，然后再添加节点
            wf.DeleteNodes();
            // 添加节点
            for (int i = 0; i < pStepNames.Count; i++)
            {
                BizWorkflowSequeuece activity = new BizWorkflowSequeuece(i + 1, pEmpIDs[i], pStepNames[i]);
                wf.AddNode(activity);
            }
        }

        public static BizTask GetTaskByID(int pTaskID)
        {
            BizTask entity = (BizTask)TaskService.GetBizEntityByID(pTaskID, typeof(BizTask));
            entity.TaskWF = (BizWorkflow)BizWorkflow.GetWorkflowByTaskID(pTaskID);

            return entity;
        }
    }
}